package com.kernowsoft.wavefx;

import com.kernowsoft.wavefx.R;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.TextView;

public class WaveFxListPager extends FragmentActivity implements
		ViewPager.OnPageChangeListener {

	private static final String TAG = WaveFxListPager.class.getSimpleName();

	private ViewPager mViewPager;
	private PagerAdapter mPagerAdapter;
	private TextView mPageNumber;

	/* for syncing */
	// The authority for the sync adapter's content provider
	public static final String AUTHORITY = "com.kernowsoft.wavefx";
	// An account type, in the form of a domain name
	public static final String ACCOUNT_TYPE = "kernowsoft.com";
	// The account name
	public static final String ACCOUNT = "dummyaccount";
	// Instance fields
	Account mAccount;

	@Override
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);

		// inflate view:
		setContentView(R.layout.layout_fx_list_view_pager);

		// create sync account:
		mAccount = CreateSyncAccount(this);
		ContentResolver.addPeriodicSync(mAccount, AUTHORITY, new Bundle(), 5000);
		//ContentResolver.setSyncAutomatically(mAccount, AUTHORITY, true);

		// get view references:
		mViewPager = (ViewPager) findViewById(R.id.fx_list_view_pager);
		Log.d(TAG, "Got ref to ViewPager: " + mViewPager);
		mViewPager.setOnPageChangeListener(this);

		mPageNumber = (TextView) findViewById(R.id.txt_current_fx);

		// instantiate view pager adapter
		mPagerAdapter = new WaveFxListPagerAdapter(getSupportFragmentManager());
		Log.d(TAG, "Got new pager adapter: " + mPagerAdapter);
		mViewPager.setAdapter(mPagerAdapter);
		onPageSelected(0); // set initial page number
	}

	/**
	 * Create a new dummy account for the sync adapter
	 * 
	 * @param context
	 *            The application context
	 */
	public static Account CreateSyncAccount(Context context) {
		// Create the account type and default account
		Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
		// Get an instance of the Android account manager
		AccountManager accountManager = (AccountManager) context
				.getSystemService(ACCOUNT_SERVICE);
		/*
		 * Add the account and account type, no password or user data If
		 * successful, return the Account object, otherwise report an error.
		 */
		if (accountManager.addAccountExplicitly(newAccount, null, null)) {
			/*
			 * If you don't set android:syncable="true" in in your <provider>
			 * element in the manifest, then call context.setIsSyncable(account,
			 * AUTHORITY, 1) here.
			 */
			Log.d(TAG, "Added new sync account");
		} else {
			/*
			 * The account exists or some other error occurred. Log this, report
			 * it, or handle it internally.
			 */
			Log.d(TAG, "Sync account already exists");
		}

		return newAccount;
	}

	private class WaveFxListPagerAdapter extends FragmentStatePagerAdapter {

		private String[] mSites;

		public WaveFxListPagerAdapter(FragmentManager fm) {
			super(fm);
			mSites = new String[] { "Croyde", "Perran", "Ekofisk", "Mountsbay" };
		}

		@Override
		public Fragment getItem(int pos) {

			Fragment f;

			f = new WaveFxListFragment();
			Log.d(TAG, "ViewPager - new fragment = " + f);
			Bundle b = new Bundle();
			b.putString("site", mSites[pos]);
			f.setArguments(b);

			return f;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
			Log.d(TAG, "ViewPager: Destroying item at " + position);
		}

		@Override
		public int getCount() {
			return mSites.length;
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		mPageNumber.setText(String.format("Site %d of %d", position + 1,
				mPagerAdapter.getCount()));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(Menu.NONE, 999, Menu.NONE, "Sync");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 999) {
			Log.d(TAG,"Requesting manual sync");
			Bundle settingsBundle = new Bundle();
			settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
			settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED,
					true);
			/*
			 * Request the sync for the default account, authority, and manual
			 * sync settings
			 */
			ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle);
			
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}
}
